package game.module.announce.dao;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import javax.sql.DataSource;

import org.apache.commons.dbutils.DbUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.ResultSetHandler;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import game.common.DaoCommonHandler;
import game.db.DataSourceManager;
import game.module.announce.bean.AnnounceBean;

public class AnnounceDao {

	private static Logger logger = LoggerFactory.getLogger(AnnounceDao.class);

	private DataSource dataSource = DataSourceManager.getInstance().getDataSource();

	static class SingletonHolder {
		static AnnounceDao instance = new AnnounceDao();
	}

	public static AnnounceDao getInstance() {
		return SingletonHolder.instance;
	}

	private ResultSetHandler<List<AnnounceBean>> multiPlayerHandler = new ResultSetHandler<List<AnnounceBean>>() {
		@Override
		public List<AnnounceBean> handle(ResultSet rs) throws SQLException {
			List<AnnounceBean> userBeanList = new ArrayList<AnnounceBean>();
			while (rs.next()) {
				AnnounceBean settingBean = new AnnounceBean();
				settingBean.setId(rs.getInt("id"));
				settingBean.setTitle(rs.getString("title"));
				settingBean.setContent(rs.getString("content"));
				userBeanList.add(settingBean);
			}
			return userBeanList;
		}
	};

	public List<AnnounceBean> getAnnounceAll() {
		List<AnnounceBean> ret = null;
		QueryRunner runner = new QueryRunner(dataSource);
		try {
			ret = runner.query("SELECT * from announce", multiPlayerHandler);
		} catch (SQLException e) {
			logger.error("", e);
			return ret;
		}
		return ret;
	}

	public void updateAnnounce(AnnounceBean settingBean) {
		QueryRunner runner = new QueryRunner(dataSource);
		try {
			runner.update("update announce set title=?,content=? where id = ?", settingBean.getTitle(),
					settingBean.getContent(), settingBean.getId());
		} catch (SQLException e) {
			logger.error("", e);
		}
	}

	public boolean addAnnounce(AnnounceBean settingBean) {
		boolean addRet = false;
		QueryRunner runner = new QueryRunner(dataSource);
		Connection conn = null;
		try {
			conn = dataSource.getConnection();
			int ret = runner.update(conn, "insert into announce(title,content) " + "values(?,?)",
					settingBean.getTitle(), settingBean.getContent());
			if (ret > 0) {
				addRet = true;
			}
			if (addRet) {
				int theId = runner.query(conn, "SELECT LAST_INSERT_ID();", DaoCommonHandler.Integerhandler);
				settingBean.setId(theId);
			}
		} catch (SQLException e) {
			logger.error("", e);
		} finally {
			DbUtils.closeQuietly(conn);
		}
		return addRet;

	}

	public void updateAnnounce(String title, String content, int aId) {
		QueryRunner runner = new QueryRunner(dataSource);
		try {
			runner.update("update announce set title=?,content=? where id = ?", title, content, aId);
		} catch (SQLException e) {
			logger.error("", e);
		}
	}

	public void deleteAnnounce(int aId) {
		QueryRunner runner = new QueryRunner(dataSource);
		try {
			runner.update("delete from announce where id = ?", aId);
		} catch (SQLException e) {
			logger.error("", e);
		}
	}

}
